<?php
/**
 * 异动流程
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Flow_Transfer");

class Flow_TransferController extends AbstractController {

    protected $flow_info;
    protected $bll_flow;

    public function handle_request() {
        $params = $this->request->get_parameters();
        if(!isset($params['flow_id'])){
            $this->show_tips('ERROR_PARAM');
        }else{
            $flow_id = intval($params['flow_id']);
        }

        //获取流程信息
        $this->bll_flow = new Bll_Flow_Info();
        $flow_info = $this->bll_flow->find_by_id($flow_id);
        $now_stage = $flow_info['flow_stage'] + 1;
        //设置打印标志
        if($flow_info['now_status'] == 2 && array_key_exists(Const_Flow::ROLE_HR, $this->user_info['role_list'])){
            $this->request->set_attribute('print_flag',1);
        }
        $this->request->set_attribute('status_flag',$flow_info['now_status']);

        //获取转岗基本信息
        $bll_tr = new Bll_Flow_Transfer();
        $transfer_info = $bll_tr->get_transfer_by_flow($flow_id);
        $transfer_info['before_info'] = explode(',', $transfer_info['before_info']);
        $transfer_info['after_info'] = explode('-', $transfer_info['after_info']);
        $this->request->set_attribute('transfer_info',$transfer_info);
        $this->request->set_attribute('user_info',$this->user_info);

        $allow_opt = array();
        //检测流程是否已经结束
        if($flow_info['now_status'] == Const_Flow::FLOW_STATUS_DOING){
            $this->_go_bak_apply_transfer($flow_info,$transfer_info['apply_user_id']);
            //获取允许编辑的input框name
            $view_flag = intval($params['view']);
            if(!$view_flag){
               $flow_stage =  $flow_info['flow_stage']+1;
            }else{
               $flow_stage =  $flow_info['flow_stage'];
            }
            $work_city_id = $flow_info['work_city_id'];
            $new_city_id=0;
            if($flow_info['flow_type']==Const_Flow::FLOW_TYPE_TRANSFER && !empty($transfer_info['new_city_id'])){
                $work_city_id = $transfer_info['new_city_id'];
                $new_city_id=$transfer_info['new_city_id'];
            }
            $allow_opt = $this->_check_auth($work_city_id, $flow_info['flow_type'], $flow_stage, $flow_id,$flow_info['work_city_id']);
            //申请人主管信息,有skip的需要回退
            $bll_hr = new Bll_Hr_OutInterface();
            $parent_info = $bll_hr->get_under_leader($transfer_info['apply_user_id']);
            //办理人主管
            $user_parent = $bll_hr->get_under_leader($transfer_info['user_id']);
            $is_sp_hr_uid = APF::get_instance()->get_config('hr_sp_uid');
            if($flow_info['flow_type']== Const_Flow::FLOW_TYPE_TRANSFER && $flow_info['flow_stage']==Const_Flow::FLOW_TRANSFER_3){
                if($this->user_info['user_id'] !=$is_sp_hr_uid){
                    $unset_a = array_search(Const_Flow::PHASE_14, $allow_opt);
                    if($unset_a!==false){
                        unset($allow_opt[$unset_a]);
                    }
                }
            }elseif($flow_info['flow_type']== Const_Flow::FLOW_TYPE_MONEY && $flow_info['flow_stage']==Const_Flow::FLOW_MONEY_3){
                if($this->user_info['user_id'] !=$is_sp_hr_uid){
                    $unset_b = array_search(Const_Flow::PHASE_13, $allow_opt);
                    if($unset_b!==false){
                        unset($allow_opt[$unset_b]);
                    }
                }
            }
            //因主管的定义发生变化原有的_check_auth已不能检测出2 ,3阶段,这里单处理
            if($view_flag){
                if($flow_info['flow_stage']==2){
                    if($user_parent && $user_parent['user_id'] == $this->user_info['user_id']){
                        $allow_opt[]=Const_Flow::PHASE_12;
                    }
                }elseif($flow_info['flow_stage']==3){
                    if($parent_info && $parent_info['user_id'] ==$this->user_info['user_id']){
                        $allow_opt[]=Const_Flow::PHASE_13;
                    }
                }
            }
        }

        if($this->request->is_post_method()){
            //如果无操作权限则跳出
            if(!$allow_opt){
                $this->show_tips('ERROR_AUTH');
            }
            $bll_hr = new Bll_Hr_OutInterface();
            foreach($allow_opt as $opt_id){
                //更新flow表用的数据
                $data = array('flow_type'=>$flow_info['flow_type'],'flow_stage'=>($flow_stage),'flow_opt'=>$opt_id);
                //更新transfer表用的数据
                $data2 = array();
                //操作成功标记位
                $error_msg = '';
                $kill_flag = false;//是否提前结束流程
                $allow_temp = $this->_get_submit_names($opt_id);
                foreach($allow_temp as $row){
                    if(in_array($row,array('effective_date'))){
                        $data2[$row] = Util_Tools::format_date($params[$row],true);
                        $data['effect_date'] = $data2[$row];
                    }else{
                        $data2[$row] = $params[$row];
                    }
                    //工资数据单独处理
                    if(in_array($row,array('hr_new_salary','hrd_new_salary'))){
                        $bll_o = new Bll_Hr_OutInterface();
                        $data2[$row] = $bll_o->encrypt_value($params[$row]);;
                    }
                    //如果HR更新关键信息，则初始化HRD的信息
                    if(in_array($row,array('hr_p_level','hr_m_level','hr_change_salary','hr_new_salary'))){
                        $main = substr($row,2);
                        $data2['hrd'.$main] = $data2[$row];
                    }

                    if(preg_match('/[a-z]*_approve.*/', $row)){
                        if($params[$row] == 2){
                            $kill_flag = true;
                        }
                    }
                }
                //更新基本信息
                $tre = $bll_tr->update_opt($flow_id,$data2);
                if(!$tre){
                    $error_msg = '&update_opt-error';
                    break;
                }
                if(!$view_flag){
                    //更新relation表
                    $rre = true;
                    if($opt_id == Const_Flow::PHASE_12){
                        $rre = $bll_tr->clear_parent_relation($flow_id);
                        if($rre && (!empty($parent_info['user_id']) && $parent_info['user_id'] != $this->user_info['user_id'])){//申请人的主管信息
                            $rre = $bll_tr->add_parent_relation($transfer_info['user_id'], $flow_id,Const_Flow::FLOW_TRANSFER_3,true,$transfer_info['apply_user_id']);
                        }
                    }elseif($opt_id == Const_Flow::PHASE_13){
                        $rre = $bll_tr->clear_parent_relation($flow_id);
                    }
                    if(!$rre){
                        $error_msg = '&update_relation-error';
                        break;
                    }

                }
                $up_flow_data = array_merge($data,$data2);
                //先记LOG,如果是同步执行的编辑，实际上获取不到role，因此save_log()方法更新LOG时不更新role_type
                $role = $this->_get_flow_step($flow_info['flow_type'],$flow_stage,$opt_id);
                $log_array = array(
                        'city_id'=>$flow_info['work_city_id'],
                        'apply_user_id'=>$this->user_info['user_id'],
                        'role_type'=>$role,
                        'flow_opt'=>$opt_id,
                        'flow_stage'=>$flow_stage,
                        'effective_date' =>isset($data2['effective_date']) ? $data2['effective_date'] : $transfer_info['effective_date']
                    );
                $skip_flow = 0;
                if($opt_id == Const_Flow::PHASE_12 && (!empty($parent_info['user_id']) && $parent_info['user_id'] == $this->user_info['user_id'])){
                    $skip_flow = 2; //这里其实两个主管是同一个人,log是重复执行了多次
                    $log_array['flow_opt'] = Const_Flow::PHASE_11;
                    $log_array['flow_stage'] = 1;//第一阶段
                    $up_flow_data['flow_stage']=3;
                    $up_flow_data['flow_opt']=13;

                }
                $lre = $bll_tr->add_opt_log(array_merge($flow_info,$data2,$log_array),$flow_id,$skip_flow);
                if(!$lre){
                    $error_msg = '&update_log-error';
                    break;
                }
                //只有办理时才更新flow表
                if(!$view_flag){
                    $fre = $this->bll_flow->update_flow($flow_id,$up_flow_data);
                    if(!$fre || in_array($fre,array(1,2))){
                        $error_msg = '&update_flow-error';
                        break;
                    }
                }

                if($kill_flag){//如果选择了驳回/撤销，则结束流程
                    $this->bll_flow->update_flow_cancel($flow_id);
                    $bll_tr->clear_parent_relation($flow_id);
                    break;
                }elseif($fre==4){//流程完成，同步到HROA
                    $re = $this->update_user_info($transfer_info);
                    if(!$re){
                        $error_msg = '&update_hroa-error';
                        break;
                    }
                }
            }

            //如果全部无错误，则成功
            $nex_url = Flow_Urls::flow_list();
            if(!$error_msg){
                $this->show_tips('OPT_SUCCESS',$nex_url,1);
            }else{
                $this->show_tips('ERROR_APPLY_TRANSFER',$nex_url.$error_msg);
            }
        }else{
            //关闭权限检测，流程变更了,原来的检测失效.20130301
            if(!$this->_check_read_flow_auth($flow_info['work_city_id'],$flow_info['flow_type'],$now_stage,$flow_info['flow_id'])){
                //$this->show_tips('ERROR_AUTH');
            }
            //职级信息
            $bll_pro = new Bll_Hr_Property();
            $this->request->set_attribute('p_level',$bll_pro->get_p_degree());
            $this->request->set_attribute('m_level',$bll_pro->get_m_degree());

            //电脑信息
            $this->request->set_attribute('computer_type', Const_Flow::computer_type());

            //构造允许操作的input的name数组
            $allow_names = array();
            if($allow_opt){
                foreach($allow_opt as $opt_id){
                    $temp = $this->_get_submit_names($opt_id);
                    $allow_names = array_merge($allow_names,$temp);
                }
            }
            $this->request->set_attribute('allow_names',$allow_names);

            return "Flow_Transfer";
        }
    }
    /**
     * 回退到申请页
     */
    private function _go_bak_apply_transfer($flow_info,$apply_user_id){
        $ceo_uid = APF::get_instance()->get_config('ceo_uid');
        $bll_hr = new Bll_Hr_OutInterface();
        $parent_info = $bll_hr->get_under_leader($flow_info['user_id']);
        //对ceo或者是有跳级的作特殊展示
        $goto=0;
        if(in_array($flow_info['flow_stage'],array(Const_Flow::FLOW_TRANSFER_3,Const_Flow::FLOW_MONEY_3)) && $ceo_uid == $apply_user_id && $this->user_info['user_id']==$ceo_uid){
            $goto = 1;
        }elseif(in_array($flow_info['flow_stage'],array(Const_Flow::FLOW_TRANSFER_2,Const_Flow::FLOW_MONEY_2)) && $parent_info['user_id']==$apply_user_id  && $this->user_info['user_id']==$apply_user_id){
            $goto = 1;
        }
        if($goto){
            $this->response->redirect(Flow_Urls::apply_transfer($flow_info['flow_id']));
        }
        return true;
    }
    /**
     * 更新HROA中的基本信息和岗位信息
     * @param unknown_type $transfer_info
     * @return boolean
     */
    private function update_user_info($transfer_info){
        $bll_o = new Bll_Hr_OutInterface();
        //更新基本信息
        $data = array();
        if(!empty($transfer_info['hrd_p_level'])){
            $data['p_level'] = $transfer_info['hrd_p_level'];
        }
        if(!empty($transfer_info['hrd_m_level'])){
            $data['m_level'] = $transfer_info['hrd_m_level'];
        }
        if(!empty($transfer_info['it_new_phone'])){
            $data['office_phone'] = $transfer_info['it_new_phone'];
        }
        if(!empty($transfer_info['it_new_phone_ex'])){
            $data['office_phone_ex'] = $transfer_info['it_new_phone_ex'];
        }
        if(!empty($transfer_info['new_job_id'])){//有填职位，那就需要更新hroa.user表work_city_id字段
            $job_data = $bll_o->get_job_dep_info($transfer_info['new_job_id']);
            if(!empty($job_data)){
                $data['work_city_id'] = $job_data['city_id'];
            }
        }
        if($data){
            $ure = $bll_o->user_update($transfer_info['user_id'],$data);
        }else{
            $ure = true;
        }
        //更新职位信息
        $parent_uid = !empty($transfer_info['new_super_uid']) ? $transfer_info['new_super_uid']: $transfer_info['apply_user_id'];
        $jre = $bll_o->update_relation($transfer_info['user_id'],$parent_uid,$transfer_info['old_job_id'],$transfer_info['new_job_id']);
        //如果填有主管id,就是更换上下级关系
        return $ure && $jre;
    }
    /**
     * 获得该操作允许的页面input的name
     * @param unknown_type $opt_id
     * @return multitype:|Ambigous <multitype:, multitype:string >
     */
    private function _get_submit_names($opt_id){
        $return = array();
        if(!$opt_id){
            return $return;
        }
        $name_list = array(
            Const_Flow::PHASE_12=>array('effective_date','old_approve','old_reason'),
            Const_Flow::PHASE_13=>array('new_approve','new_reason'),
            Const_Flow::PHASE_14=>array('hr_approve','hr_p_level','hr_m_level','hr_reason','hr_change_salary','hr_new_salary'),
            Const_Flow::PHASE_15=>array('hrd_approve','hrd_p_level','hrd_m_level','hrd_reason','hrd_change_salary','hrd_new_salary'),
            Const_Flow::PHASE_16=>array('admin_change_seat','admin_new_seat','admin_change_computer','admin_new_computer','admin_remark'),
            Const_Flow::PHASE_17=>array('it_change_power','it_change_computer','it_revoke_computer','it_change_phone','it_new_phone','it_new_phone_ex','it_remark'),
            Const_Flow::PHASE_18=>array('hr_approve_all','hr_approve_remark'),
        );
        if(isset($name_list[$opt_id])){
            $return = $name_list[$opt_id];
        }

        return $return;
    }
    /**
     * 检测参数
     * @param unknown_type $params
     * @return boolean
     */
    private function _check_params($params){
        $return = false;
        if(!isset($params['flow_id'])){
            return $return;
        }else{
            return true;
        }
    }
}
